package com.aaa.interceptors;

import com.aaa.entity.Users;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 用户登录校验器
 */
public class LoginInterceptor  implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //先获取session对象
        HttpSession session=request.getSession();
        //获取session中的用户对象
        Users sessionUser= (Users) session.getAttribute("SESSION_USER");
        //判断sessionUser如果不存在
        if (sessionUser==null){
            response.sendRedirect("/toLogin");
            return false;
        }

        return true;
    }
}
